package com.ry.widget;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

/**
 *
 * 高斯模糊-圆形外面
 * Create by renxianzuo on 2020/12/4
 * Describe
 */
public class MyMaskFilterView extends androidx.appcompat.widget.AppCompatTextView {
    private Paint lightPaint;
    private Paint defaultPaint;
    /** 发光范围 */
    private int radioRadius;
    private Rect rect = new Rect();
    private int mBackgroundColor = 0xffE599FE;

    public MyMaskFilterView(Context context) {
        super(context);
        init(context);
    }

    public MyMaskFilterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        radioRadius = dipToPx(context,7);
        lightPaint = new Paint();
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        lightPaint.setColor(0x80ffffff);
        lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.OUTER));

        defaultPaint = new Paint();
        defaultPaint.setAntiAlias(true);
        defaultPaint.setStyle(Paint.Style.FILL);
        defaultPaint.setColor(mBackgroundColor);
        defaultPaint.setStrokeWidth(dipToPx(context,1f));

        setBlurType(2);
    }

    public void setBlurType(int blurType) {
        switch (blurType) {
            case 0:
                lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.INNER));
                break;
            case 1:
                lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.NORMAL));
                break;
            case 2:
                lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.SOLID));
                break;
            case 3:
                lightPaint.setMaskFilter(new BlurMaskFilter(radioRadius, BlurMaskFilter.Blur.OUTER));
                break;
        }

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //设置画得一个半径，然后比较长和宽，以最大的值来确定长方形的长宽，确定半径
        int min = Math.min(width, height);
        rect.set(getPaddingLeft(), getPaddingTop(), min - getPaddingRight(), min - getPaddingBottom());

        int center_x = width / 2;
        int center_y = height / 2;

        int r = rect.width()/2;

        canvas.drawCircle(center_x, center_y, r, lightPaint);

        canvas.drawCircle(center_x, center_y, r, defaultPaint);

        super.onDraw(canvas);
    }


    public void setCircleBackgroundColor(int color){
        defaultPaint.setColor(color);
        invalidate();
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

